package com.pr.makfilm.user.pages.room.list.cmp;

import com.pr.makfilm.core.ajax.ConfirmationCallListener;
import com.pr.makfilm.core.cmp.button.BootstrapLink;
import com.pr.makfilm.core.cmp.notify.Notification;
import com.pr.makfilm.core.cmp.table.AjaxDataTable;
import com.pr.makfilm.core.cmp.table.toolbar.FilterToolbar;
import com.pr.makfilm.domain.Room;
import com.pr.makfilm.domain.filter.OpenSearchDescription;
import com.pr.makfilm.service.IRoomService;
import com.pr.makfilm.user.navigation.NavigateToRoomDetailsPage;
import com.pr.makfilm.user.pages.room.create.CreateRoomPage;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Pawel
 * Date: 27.09.13
 * Time: 00:54
 * To change this template use File | Settings | File Templates.
 */
public class RoomFilterToolbar extends FilterToolbar<Room>
{
   @SpringBean
   private IRoomService roomService;

   private AjaxLink<Void> newRoom;

//   private BootstrapLink editRoom;

   private BootstrapLink deleteRoom;

   private BootstrapLink roomDetails;

   private List<String> cities;

   public RoomFilterToolbar(final AjaxDataTable<Room> table, IModel<Room> model)
   {
      super(table);
      setOutputMarkupId(true);
      setDefaultModel(model);
   }

   @Override
   protected void onInitialize()
   {
      super.onInitialize();
      newRoom = new AjaxLink<Void>("new")
      {
         @Override
         public void onClick(AjaxRequestTarget target)
         {
            setResponsePage(CreateRoomPage.class);
         }
      };
      add(newRoom);
//      editRoom = new BootstrapLink("edit")
//      {
//         @Override
//         public void onClick(AjaxRequestTarget target)
//         {
//            window.resetState(false);
//            window.setSchool(getSelected());
//            window.show(target);
//         }
//
//         @Override
//         protected void onConfigure()
//         {
//            setEnabled(getSelected() != null);
//         }
//      };
//      add(editRoom);
      roomDetails = new BootstrapLink("details")
      {
         @Override
         public void onClick(AjaxRequestTarget target)
         {
            new NavigateToRoomDetailsPage(getSelected()).navigate();
         }

         @Override
         protected void onConfigure()
         {
            setEnabled(getSelected() != null);
         }
      };
      add(roomDetails);

      deleteRoom = new BootstrapLink("delete")
      {
         @Override
         public void onClick(AjaxRequestTarget target)
         {
            roomService.delete(getSelected());
            Notification.success(getString("room.successfully.deleted"));
            refreshTableView(target);
         }

         @Override
         protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
         {
            super.updateAjaxAttributes(attributes);
            attributes.getAjaxCallListeners().add(new ConfirmationCallListener(getString("room.delete.confirm")));
         }

         @Override
         protected void onConfigure()
         {
            setEnabled(getSelected() != null);
         }
      };
      add(deleteRoom);

   }

   @Override
   public void registerFilters(DataTable<?, ?> table, OpenSearchDescription<Room> osd)
   {
      addFilter(new TextField<String>("name", new PropertyModel<String>(osd, "filter.name")), getString("name"));
      DropDownChoice<String> dropDownChoice = new DropDownChoice<String>("city",
              new PropertyModel<String>(osd, "filter.city"), new PropertyModel<List<String>>(this, "cities"));
      dropDownChoice.setNullValid(true);
      addFilter(dropDownChoice, getString("city"));
   }

   private Room getSelected()
   {
      return (Room) getDefaultModelObject();
   }

   private void refreshTableView(AjaxRequestTarget target)
   {
      target.add(table);
//      target.add(editRoom);
      target.add(deleteRoom);
      target.add(roomDetails);
   }

   @Override
   protected void onConfigure()
   {
      cities = roomService.findRoomCities();
   }
}
